package de.lulusoft.anothertorrent.gui.commands;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.IHandlerListener;

import de.lulusoft.anothertorrent.core.rtorrent.Torrent;

public class ChangeTorrentPriorityCommand implements IHandler {
	
	public static final String ID = "de.lulusoft.anothertorrent.gui.commands.ChangeTorrentPriorityCommand";

	public static final String P_TORRENT = "torrentParameter";
	public static final String P_PRIORITY = "priorityParameter";
	
	@Override
	public void addHandlerListener(IHandlerListener handlerListener) {
	}

	@Override
	public void dispose() {
	}

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		Torrent t = (Torrent)event.getTrigger();
		Long val = Long.parseLong(event.getParameter(P_PRIORITY));
		// Send cmd to server
		t.getServer().getRTorrentManager().torrentSetPriority(t.getHash(), val);
		// Change local cached data to reflect the changes immediately
		t.setPriority(val);
		switch (val.intValue())
		{
		case 0: t.setPriority_str("off"); break;
		case 1: t.setPriority_str("low"); break;
		case 2: t.setPriority_str("normal"); break;
		case 3: t.setPriority_str("high"); break;
		}
		return null;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

	@Override
	public boolean isHandled() {
		return true;
	}

	@Override
	public void removeHandlerListener(IHandlerListener handlerListener) {
	}

}
